﻿//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Author: Jeronimo Milea
//
// Copyright 2007-2011 - Jeronimo Milea

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;
using System.Collections;

namespace NetJsWire.Widgets.Ui.Form
{
    public partial class ComboBoxColumnEditorDialog : System.Windows.Forms.Form
    {
        // Fields
        private const short EditableDynamic = 0;
        private const short EditableNo = 2;
        private short editableState;
        private const short EditableYes = 1;
        private ComboBoxColumnEditor editor;
        private object value;
        private Ui.Form.ComboBox combobox;
        private static Type iComponentChangeServiceType;
        private ComboBoxColumnCollection _columns;

        // Methods

        public ComboBoxColumnEditorDialog()
        {
            iComponentChangeServiceType = typeof(IComponentChangeService);
            InitializeComponent();
        }

        public void SetComboBox(Ui.Form.ComboBox combo)
        {
            combobox = combo;
            _columns = new ComboBoxColumnCollection(combobox);
            //PopulateColumns();
        }


        public ComboBoxColumnEditorDialog(CollectionEditor editor)
        {
            this.editor = (ComboBoxColumnEditor)editor;
            this.InitializeComponent();
            //this.HookEvents();
        }

        protected bool CanRemoveInstance(object value)
        {
            return this.editor.CanRemoveInstance(value);
        }

        protected virtual bool CanSelectMultipleInstances()
        {
            return this.editor.CanSelectMultipleInstances();
        }

        protected object CreateInstance(Type itemType)
        {
            return this.editor.CreateInstance(itemType);
        }

        protected void DestroyInstance(object instance)
        {
            //this.editor.DestroyInstance(instance);
        }

        protected virtual void DisplayError(Exception e)
        {
            IUIService service = (IUIService)this.GetService(typeof(IUIService));
            if (service != null)
            {
                service.ShowError(e);
            }
            else
            {
                string message = e.Message;
                if ((message == null) || (message.Length == 0))
                {
                    message = e.ToString();
                }
                System.Windows.Forms.MessageBox.Show(null, message, null, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, 0);
            }
        }

        protected override object GetService(Type serviceType)
        {
            return this.editor.GetService(serviceType);
        }

        protected void OnEditValueChanged()
        {
            object[] items = Items;
            string text = string.Empty;
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] is string)
                {
                    text = text + ((string)items[i]);
                    if (i != (items.Length - 1))
                    {
                        text = text + "\r\n";
                    }
                }
            }
            //this.textEntry.Text = text;
        }

        protected internal virtual DialogResult ShowEditorDialog(IWindowsFormsEditorService edSvc)
        {
            return edSvc.ShowDialog(this);
        }

        // Properties
        internal virtual bool CollectionEditable
        {
            get
            {
                if (this.editableState != 0)
                {
                    return (this.editableState == 1);
                }
                bool flag = typeof(IList).IsAssignableFrom(this.editor.CollectionType);
                if (flag)
                {
                    IList list = this.EditValue as IList;
                    if (list != null)
                    {
                        return !list.IsReadOnly;
                    }
                }
                return flag;
            }
            set
            {
                if (value)
                {
                    this.editableState = 1;
                }
                else
                {
                    this.editableState = 2;
                }
            }
        }

        protected Type CollectionItemType
        {
            get
            {
                return this.editor.CollectionItemType;
            }
        }

        protected Type CollectionType
        {
            get
            {
                return this.editor.CollectionType;
            }
        }

        protected ITypeDescriptorContext Context
        {
            get
            {
                return this.editor.Context;
            }
        }

        public object EditValue
        {
            get
            {
                return this.value;
            }
            set
            {
                this.value = value;
                this.OnEditValueChanged();
            }
        }

        protected object[] Items
        {
            get
            {
                return this.editor.GetItems(this.EditValue);
            }
            set
            {
                bool flag = false;
                try
                {
                    flag = this.Context.OnComponentChanging();
                }
                catch (Exception exception)
                {
                    //if (ClientUtils.IsCriticalException(exception))
                    //{
                    //    throw;
                    //}
                    this.DisplayError(exception);
                }
                if (flag)
                {
                    object obj2 = this.editor.SetItems(this.EditValue, value);
                    if (obj2 != this.EditValue)
                    {
                        this.EditValue = obj2;
                    }
                    this.Context.OnComponentChanged();
                }
            }
        }

        protected Type[] NewItemTypes
        {
            get
            {
                return this.editor.NewItemTypes;
            }
        }
    }
}
